const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
const app = getApp();
Page({
  data: {
    logininfo: {},
    //收藏隐私
    collectionSetup: 0,
    //圈子隐私类型
    circleSetup: 0,
    //公开圈子的面板显示
    detailShow: true,
  },
  onLoad: function () {
    var _this = this;
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
    });
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.GetUserSetup, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              collectionSetup: res.result.entity.collectionSetup,
              circleSetup: res.result.entity.circleSetup
            })
          }
        });
        util.post(api.GetCircleByUserJoin, data).then(function (res) {
          if (res.code == 200) {
            var arr = res.result.list
            var openCircle = []
            var privacyCircle = []
            for (var i=0;i<arr.length;i++){
              if (arr[i].myprivate==0){
                openCircle.push(arr[i])
              }else{
                privacyCircle.push(arr[i])
              }
            }
            _this.setData({
              openCircle: openCircle,
              privacyCircle: privacyCircle
            })
            console.log(openCircle)
            console.log(privacyCircle)
          }
        });
      });
    }
  },
  // 控制收藏隐私
  switchChange: function () {
    var _this = this
    var collectionSetup = _this.data.collectionSetup;
    if (collectionSetup == 0) {
      collectionSetup = 1
    } else {
      collectionSetup = 0
    }
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        data.param = JSON.stringify({ "collectionSetup": collectionSetup })
        util.post(api.UpateUserSetup, data).then(function (res) {
          if (res.code == 200) {
            _this.onLoad();
          }
        });
      });
    }
  },
  // 获取圈子隐私选择
  setopen: function (e) {
    var _this = this
    var selId = e.target.dataset.id
    console.log(selId)
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        data.param = JSON.stringify({ "circleSetup": selId })
        util.post(api.UpateUserSetup, data).then(function (res) {
          if (res.code == 200) {
            _this.onLoad();
          }
        });
      });
    }
  },
  // 控制选择圈子日记面板显示
  detailopen: function () {
    var show = this.data.detailShow;
    if (show) {
      this.setData({
        detailShow: false
      })
    } else {
      this.setData({
        detailShow: true
      })
    }
  },
  // 修改日记隐私
  switchdetails: function () {
  }
})
